<?php

namespace App\Http\Controllers\Estate;

use App\Http\Controllers\Controller;
use App\Models\ElectricAccountBank;
use App\Models\ElectricAccountType;
use App\Models\ElectricAgentLevel;
use App\Models\ElectricPlatformAgent;
use App\Models\SysRegion;
use Illuminate\Http\Request;

class ElectricPlatformAgentController extends Controller
{
    public function index(Request $request)
    {
        $agent_name = $request->get('agent_name');
        $agent_mobile = $request->get('agent_mobile');
        $login_account = $request->get('login_account');

        $where = [];
        if (isset($agent_name)) {
            $where[] = ['agent_name', '=', $agent_name];
        }

        if (isset($agent_mobile)) {
            $where[] = ['agent_mobile', '=', $agent_mobile];
        }

        if (isset($login_account)) {
            $where[] = ['login_account', '=', $login_account];
        }

        $limit = $request->get('limit', 20);
        $res = ElectricPlatformAgent::query()->where($where)->select('id', 'agent_name', 'agent_mobile', 'login_account', 'status', 'province', 'city', 'area', 'detail', 'create_time')->paginate($limit)->toArray();

        $data = &$res['data'];

        $sys_region_ = SysRegion::query()->select('id', 'name')->get()->toArray();

        $sys_region = array_column($sys_region_, 'name', 'id');

        foreach ($data as $index => $datum) {
            $province = '';
            $city = '';
            $area = '';
            if ($datum['province'] > 0) {
                $province = $sys_region[$datum['province']];
            }
            if ($datum['city'] > 0) {
                $city = $sys_region[$datum['city']];
            }
            if ($datum['area'] > 0) {
                $area = $sys_region[$datum['area']];
            }
            $data[$index]['full_address'] = $province . " - " . $city . " - " . $area . " - " . $datum['detail'];
        }

        return $this->success($res);
    }

    public function show($id)
    {
        $field = ['id', 'agent_name', 'agent_mobile', 'agent_idcard', 'agent_type', 'level', 'sub_mch_id', 'province', 'city', 'area', 'detail', 'agent_remarks', 'split_type', 'wechat_account_type_id', 'wechat_account', 'alipay_account_type_id', 'alipay_account', 'alipay_name', 'rate', 'account_bank', 'else_discount', 'bank_user_name', 'bank_card_number', 'rate2'];
        $data = ElectricPlatformAgent::query()->select($field)->where('id', $id)->first();

        return $this->success($data);
    }

    public function update(Request $request, $id)
    {
        $data = $request->post();
        [$data['province'], $data['city'], $data['area']] = $data['loadOptions'];
        unset($data['loadOptions']);

        $res = ElectricPlatformAgent::query()->where('id', $id)->update($data);
        return $res ? $this->success() : $this->error();
    }

    /**
     * @param Request $request
     * 添加代理商
     */
    public function store(Request $request)
    {
        try {
            $data = $request->post();
            [$data['province'], $data['city'], $data['area']] = $data['loadOptions'];
            unset($data['loadOptions']);
            ElectricPlatformAgent::query()->create($data);
        } catch (\Exception $exception) {
            return $this->error('请输入完整');
        }
        return $this->success();

    }

    /**
     *禁用或启用代理商
     */
    public function status(Request $request)
    {
        $id = $request->get('id');
        $state = ElectricPlatformAgent::query()->where('id', $id)->value('status');

        $res = ElectricPlatformAgent::query()->where('id', $id)->update(['status' => $state ? 0 : 1]);

        return $res ? $this->success() : $this->error();
    }

    /**
     * 删除
     */
    public function destroy($ids)
    {
        $ids = explode(',', $ids);

        $res = ElectricPlatformAgent::query()->whereIn('id', $ids)->delete();
        return $res ? $this->success() : $this->error();
    }

    /**
     * 获取代理商等级列表
     */
    public function agentLevel()
    {
        $list = ElectricAgentLevel::query()->select('id as value', 'level_name as label')->where('status', 1)->get();
        return $this->success($list);
    }

    /**
     * 获取省市区
     */
    public function region(Request $request)
    {
        $data = SysRegion::query()->where('level_id', '<>', 0)->select('id', 'name', 'parent_id')->orderBy('id', 'asc')->get()->toArray();
        $res = self::tree($data, 10000000);

        return $this->success($res);
    }

    /**
     * 递归
     * @param $data
     * @param int $pid
     * @return array
     */
    private static function tree($data, $pid = 0)
    {
        $res = array();

        foreach ($data as $value) {
            if ($value['parent_id'] == $pid) {
                $children = self::tree($data, $value['id']);
                if (!empty($children)) {
                    $value['children'] = $children;
                }
                $res[] = $value;
            }
        }
        return $res;
    }

    /**
     * 获取选项
     */
    public function option()
    {
        $wechat = ElectricAccountType::query()->where('status', 1)->select('type_name', 'description', 'id')->where('flag', 2)->get();

        $alipay = ElectricAccountType::query()->where('status', 1)->select('type_name', 'description', 'id')->where('flag', 1)->get();

        //开户行
        $bank = ElectricAccountBank::query()->where('status', 1)->select('bank_name', 'bank_code')->get();
        return $this->success(['wechat' => $wechat, 'alipay' => $alipay, 'bank' => $bank]);
    }
}
